package com.gdpu.controller;

import com.gdpu.api.CommonResult;
import com.gdpu.config.JwtConfig;
import com.gdpu.dto.UmsAdminParam;
import com.gdpu.service.UmsAdminService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * @Author:weiming
 * @Date:2021/5/27
 */
@Api(tags = "管理员用户模块")
@RestController
public class UmsAdminController {

    @Autowired
    private JwtConfig jwtConfig;

    @Autowired
    private  UmsAdminService umsAdminService;
    @Autowired
    private PasswordEncoder passwordEncoder;


    @ApiOperation(value = "登录以后返回token")
    @PostMapping("/login")
    public CommonResult login(@RequestBody UmsAdminParam umsAdminLoginParam){
        String token = umsAdminService.login(umsAdminLoginParam.getUsername(), umsAdminLoginParam.getPassword());
        if (token == null) {
            return CommonResult.validateFailed("用户名或密码错误");
        }
        Map<String, String> tokenMap = new HashMap<>(2);
        tokenMap.put("token",token);
        tokenMap.put("tokenHead", jwtConfig.tokenHead);
        return CommonResult.success(tokenMap, "登录成功");
    }

    @ApiOperation(value = "用户注册")
    @PostMapping("/regist")
    public CommonResult regist(@RequestBody UmsAdminParam umsAdminLoginParam){
        int count  =  umsAdminService.regist(umsAdminLoginParam.getUsername(), umsAdminLoginParam.getPassword());
        if (count != 0) {
            return CommonResult.validateFailed("注册失败");
        }
        return CommonResult.success("注册成功");
    }




    @GetMapping("/info")
    public CommonResult info() {
        Map map = new HashMap(3);
        map.put("roles", "[admin]");
        map.put("name", "admin");
        map.put("avatar", "https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif");
        return CommonResult.success(map);
    }


}
